package aduros.game {

public class Job
{
    public var func :Function;

    public var minute :int;
    public var hour :int;
    public var dayOfMonth :int;
    public var month :int;

    public function Job (func :Function,
        minute :int = -1, hour :int = -1, dayOfMonth :int = -1, month :int = -1) :void
    {
        this.func = func;
        this.minute = minute;
        this.hour = hour;
        this.dayOfMonth = dayOfMonth;
        this.month = month;
    }

    protected static function delta (x :Number, y :Number, base :int) :Number
    {
        if (x > y) {
            return base - x + y;
        } else {
            return y - x; 
        }
    }

    public function timeUntil (now :Date) :Number
    {
        var minutesFromNow :Number = 0;

        if (minute >= 0) {
            minutesFromNow += delta(now.minutes, minute, 60);
        }
        if (hour >= 0) {
            minutesFromNow += 60 * delta(now.hours, hour, 60);
        }
        if (dayOfMonth >= 0) {
            minutesFromNow += 24*60 * delta(now.date, dayOfMonth, 30);
        }
        if (dayOfMonth >= 0) {
            minutesFromNow += 30*24*60 * delta(now.month, month, 12);
        }

        var offset :Number = 1000*(60-now.seconds) + (1000-now.milliseconds);

        return 60*1000*minutesFromNow + offset;
    }
}

}
